<script type="text/javascript">
	//表格查询  
	function reloadMaterialCategory() {
		var params = materialCategory_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		materialCategory_list.treegrid('reload'); //设置好查询参数 reload 一下就可以了  
		materialCategory_list.treegrid('unselectAll');
		materialCategory_list.treegrid('uncheckAll');
// 		clearMaterialSearch();
	}

	function addMaterialCategory() {
		var rows = materialCategory_list.datagrid('getChecked');
		printLog(rows);
		var mcParentId = -1;
		if (rows && rows.length > 0) {
			if (rows.length != 1) {
				showMsg('至多只能选择一个父类别记录.');
				return;
			} else {
				mcParentId = rows[0].id;
			}
		}
		createFromWindow("新增信息",
				"materialCategory.do?editMaterialCategory&mcParentId="
						+ mcParentId,
				"materialCategory.do?saveMaterialCategory", function(data) {
					reloadMaterialCategory();
				});
	}

	function deleteMaterialCategory() {
		var rows = materialCategory_list.datagrid('getChecked');
		if (rows && rows.length > 0) {
// 			$.messager.confirm('询问', '你确认要删除吗?', function(r) {
// 				if (r) {
// 					var ids = "";
// 					for ( var i = 0; i < rows.length; i++) {
// 						ids += rows[i].id + ",";
// 					}
// 					$.post('materialCategory.do?deleteMaterialCategoryItems', {
// 						ids : ids
// 					}, function(result) {
// 						if (result.success) {
// 							reloadMaterialCategory();
// 						}
// 						showMsg(result.msg);
// 					}, 'json');
// 				}
// 			});
			
			$.messager
			.confirm(
					'询问',
					'你删除该类别后，对应其下的子类别和耗材列表会同时删除！你确认要删除吗?',
					function(r) {
						if (r) {
							var ids = "";
							for ( var i = 0; i < rows.length; i++) {
								ids += rows[i].id + ",";
							}
							$.post(
											'materialCategory.do?deleteMaterialCategoryItems',
											{
												ids : ids
											},
											function(result) {
												if (result.success) {
													reloadMaterialCategory();
												}
												showMsg(result.msg);
											}, 'json');
						}
					});
		} else {
			showMsg("请选择操作记录.");
		}
	}

	function editMaterialCategory() {
		var rows = materialCategory_list.datagrid('getChecked');
		printLog(rows);
		if (rows && rows.length > 0) {
			if (rows.length > 1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindow("编辑信息",
					"materialCategory.do?editMaterialCategory&materialCategoryId="
							+ rows[0].id,
					"materialCategory.do?saveMaterialCategory", function(data) {
						reloadMaterialCategory();
					}, true);
		} else {
			showMsg('请选择操作记录.');
		}

	}

	//操作按钮触发
	function operationsLookMaterialCategory(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = materialCategory_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("查看信息",
					"materialCategory.do?editMaterialCategory&materialCategoryId="
							+ id, null, null, false);
		} else {
			showMsg('请选择查看记录.');
		}
	}

	function operationsEditMaterialCategory(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = materialCategory_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("编辑信息",
					"materialCategory.do?editMaterialCategory&materialCategoryId="
							+ id, "materialCategory.do?saveMaterialCategory",
					function(data) {
						reloadMaterialCategory();
					}, true);
		} else {
			showMsg('请选择编辑记录.');
		}
	}

	function operationsSendMaterialCategory() {
		printLog('发送');
	}

	function operationsDeleteMaterialCategory(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = new_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			$.messager
					.confirm(
							'询问',
							'你删除该类别后，对应其下的子类别和耗材列表会同时删除！你确认要删除吗?',
							function(r) {
								if (r) {
									$
											.post(
													'materialCategory.do?deleteMaterialCategoryItems',
													{
														ids : id
													},
													function(result) {
														if (result.success) {
															reloadMaterialCategory();
														}
														showMsg(result.msg);
													}, 'json');
								}
							});
		} else {
			showMsg("请选择操作记录.");
		}
	}

	function clearMaterialCategorySearch() {
		setTreeGridSearchParams('materialCategory_list',
				'materialCategorySearchForm', true, reloadMaterialCategory);
	}

	//根据条件查询
	function materialCategorySearch() {
		//		var params = $('#materialCategory_list').treegrid('options').queryParams; 
		//		printLog(params);
		setTreeGridSearchParams('materialCategory_list',
				'materialCategorySearchForm', false, reloadMaterialCategory);
	}

	function onClickMaterialCategoryItem(rowData) {
		printLog(rowData);
		var params = $('#material_list').datagrid('options').queryParams;
		params['materialCategoryId'] = rowData.id;
		//设置好查询参数 reload 一下就可以了 
		$('#material_list').datagrid('reload');
	}
	
	function onDblClickMaterialCategoryRow(rowData) {
		createFromWindow("编辑信息",
				"materialCategory.do?editMaterialCategory&materialCategoryId="
						+ rowData.id,
				"materialCategory.do?saveMaterialCategory", function(data) {
					reloadMaterialCategory();
				}, true);
	}
</script>



<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false">

		<@eu.treeGrid id="materialCategory_list"
		toolbar="#materialCategorylist_search" idField="id"
		url="materialCategory.do?loadTreeList" onDblClickRow="onDblClickMaterialCategoryRow(rowData)"
		item="com.xdtech.ad.vo.MaterialCategoryItem"/>

		<div id="materialCategorylist_search">
			<div style="margin-bottom: 5px; padding-left: 5px; margin-top: 5px;">
				<form id="materialCategorySearchForm">
					<!-- 防止form表单里只有一个text元素时候，按enter键自动提交问题 -->
					<input style="display: none" />
					<table>
						<tr>
							<td>名称：</td>
							<td><input class="easyui-validatebox textbox" name="name" /></td>
							<td><@eu.linkbutton showName="查询" iconCls="icon-search"
								onclick="materialCategorySearch()"/> <@eu.linkbutton
								showName="清空" iconCls="icon-clear"
								onclick="clearMaterialCategorySearch()"/></td>
						</tr>
					</table>
				</form>

			</div>
			<div id="materialCategory_toolbar" style="height: auto">
				<@eu.linkbutton showName="新增" shiro="ad:materialCategory:add"
				iconCls="icon-add" onclick="addMaterialCategory()"/> <@eu.linkbutton
				showName="删除" shiro="ad:materialCategory:delete"
				iconCls="icon-remove" onclick="deleteMaterialCategory()"/>
				<@eu.linkbutton showName="编辑" shiro="ad:materialCategory:edit"
				iconCls="icon-edit" onclick="editMaterialCategory()"/>
				<@eu.linkbutton showName="刷新" shiro="ad:materialCategory:refresh"
				iconCls="icon-reload" onclick="reloadMaterialCategory()"/></div>
		</div>
	</div>
</div>